
import conf from '../config/userconf.js'
export default{
    data(){
        var validateTel = (rule,value,callback) => {    //正则手机号

            var MobileRegex = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
            if (!MobileRegex.test(value)) {
                callback(new Error('手机号格式不正确！'))
            } else {
                callback();
            }
        };
        var validatePass = (rule, value, callback) => {   //验证密码
            if (value === '') {
              callback(new Error('请输入密码'));
            } else {
              if (this.password.a_password_again !== '') {
                this.$refs.password.validateField('a_password_again');
              }
              callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {  //验证密码
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.password.a_password) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };

        return{
            list:[],  //列表数据
            searchData:{ //查询
                anm_branch_name:''
            },
            form: {    //账号新增、修改  表单数据
                a_account:'',
                a_user_name:'',
                a_sex:'',
                a_phone:'',
                a_emergency:'',
                a_emergency_phone:'',
                a_address:'',
                a_native_place:'',
                a_status:'',
            },
            password: {    //修改密码   表单数据
                oldpass: '',
                a_password: '',
                a_password_again: '',
            },
            dialogtitle: '添加账号',  //弹框标题
            dialogFormVisible: false,  //嵌套表格隐藏展示
            editPassVisible: false,  //修改密码  隐藏
            rules:{   //表单验证
                a_account: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                ],
                a_user_name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' },
                    { min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'blur' },
                    { pattern: /^[\u4E00-\u9FA5]+$/, message: '姓名只能为中文'}
                ],
                a_sex: [
                    { required: true, message: '请选择性别', trigger: 'change' }
                ],
                a_phone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { validator: validateTel, trigger: 'blur'}
                ],
                a_emergency: [
                    { required: true, message: '请输入紧急联系人', trigger: 'blur' },
                    { min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'blur' },
                    { pattern: /^[\u4E00-\u9FA5]+$/, message: '姓名只能为中文'}
                ],
                a_emergency_phone: [
                    { required: true, message: '请输入紧急联系人手机号', trigger: 'blur' },
                    { validator: validateTel, trigger: 'blur'}
                ],
                a_address: [
                    { required: true, message: '请输入现住地址', trigger: 'blur' }
                ],
                a_native_place: [
                    { required: true, message: '请输入籍贯', trigger: 'blur' }
                ],
                a_status: [
                    { required: true, message: '请选择账号状态', trigger: 'change' }
                ],
            },
            edit_id: '',  //编辑    账号id
            rulePass: {  //修改密码 验证
                a_password: [
                    { required: true, validator: validatePass, trigger: 'blur' },
                    { min: 6, message: '密码不少于6位', trigger: 'blur' }
                ],
                a_password_again: [
                    { required: true, validator: validatePass2, trigger: 'blur' },
                    { min: 6, message: '密码不少于6位', trigger: 'blur' }
                ],
                oldpass: [
                    { required: true, message: '请输入旧密码', trigger: 'blur' },
                    { min: 6, message: '密码不少于6位', trigger: 'blur' }
                ]
            }
        }
    },
    filters:{

    },
    methods:{
        /* 列表*/
        getList (data=null){
            conf.account.list(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                    this.list = res.data.data.data
                }
            })
        },
        //增
        add(){
            // 设置弹框标题
            this.dialogtitle = '添加账号';
            this.dialogFormVisible = true;  
        },
        //修改账号信息（编辑）
        edit(val) {
            this.form = {
                a_account: val.a_account,
                a_user_name: val.a_user_name,
                a_sex: val.a_sex ? val.a_sex.toString() : '',
                a_phone: val.a_phone,
                a_emergency: val.a_emergency,
                a_emergency_phone: val.a_emergency_phone,
                a_address: val.a_address,
                a_native_place: val.a_native_place,
                a_status: val.a_status ? val.a_status.toString() : '',
            },
            this.edit_id = val.a_id;
            // 设置弹框标题
            this.dialogtitle = '编辑账号';
            this.dialogFormVisible = true;  
        },
        //修改密码
        editPass(val){
            this.edit_id = val.a_id;
            this.editPassVisible = true;
        },
        // 修改密码  提交
        submitPass(formName){
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    const data = this.password;
                    data.a_id = this.edit_id;
                    conf.account.editPass(data,(state,res)=>{
                        if(state == 'success'&&res.data.code == 0){
                            this.$message({
                                type: 'success',
                                message: '修改成功'
                            });
                            this.editPassVisible = false
                        }else{
                            this.$message({
                                type: 'info',
                                message: res.data.msg
                            });
                        }
                    })
                }else{
                    // console.log('error submit!!');
                    return false;
                }
            })
        },
        // 查
        search () {
            let data  = this.searchData;

            this.getList(data)
            // this.$router.push({name:'pay'})
        },
        // 账号状态
        accountState (row, column, cellValue, index) {
            switch (cellValue) {
                case 1:
                    return '启用'
                case 2:
                    return '禁用'
            }
        },
        // 性别
        sex (row, column, cellValue, index) {
            switch (cellValue) {
                case 1:
                    return '男'
                case 2:
                    return '女'
            }
        },
        //表单提交
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
              if (valid) {
                if (this.dialogtitle == '添加账号') {
                    //请求
                    conf.account.add(this.form,(state,res)=>{
                        if(state == 'success'&&res.data.code == 0){
                            this.getList();
                            this.$message({
                                type: 'success',
                                message: '添加成功'
                            });
                            this.dialogFormVisible = false
                        }else{
                            this.$message({
                                type: 'info',
                                message: res.data.msg
                            });
                        }
                    })
                }else{  //编辑账号信息
                    //请求
                    const data = this.form;
                    data.a_id = this.edit_id;
                    delete data.a_account;
                    conf.account.edit(data,(state,res)=>{
                        if(state == 'success'&&res.data.code == 0){
                            this.getList();
                            this.$message({
                                type: 'success',
                                message: '编辑成功'
                            });
                            this.dialogFormVisible = false
                        }else{
                            this.$message({
                                type: 'info',
                                message: res.data.msg
                            });
                        }
                    })
                }
                    
              } else {
                // console.log('error submit!!');
                return false;
              }
            });
        },
        // 取消提交
        cancel(formName){
            this.dialogFormVisible = false;
            if (formName) {
                this.$refs[formName].resetFields();
            }
        },
        // 修改密码   取消提交
        cancelPass(formName){
            this.editPassVisible = false;
            if (formName) {
                this.$refs[formName].resetFields();
            }
        }
    },
    mounted (){
        this.$nextTick(function(){
            this.getList()
        })
    },
}